#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use serde::Deserialize;
    use attestation_common::ConfigSingleton;

    #[derive(Debug, Deserialize, PartialEq)]
    struct TestConfig {
        name: String,
        version: Option<String>,
        #[serde(default = "default_environment")]
        environment: String,
        #[serde(default)]
        debug_mode: bool,
        #[serde(default)]
        timeout_seconds: u32,
        database: Option<DatabaseConfig>,
        features: Features,
    }

    fn default_environment() -> String {
        "development".to_string()
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct DatabaseConfig {
        host: String,
        port: u16,
        #[serde(default = "default_db_name")]
        name: String,
        #[serde(default)]
        max_connections: u32,
    }

    fn default_db_name() -> String {
        "default_db".to_string()
    }

    #[derive(Debug, Deserialize, PartialEq, Default)]
    struct Features {
        #[serde(default)]
        enable_logging: bool,
        #[serde(default)]
        enable_caching: bool,
        #[serde(default)]
        enable_metrics: bool,
        #[serde(default)]
        log_level: String,
    }

    fn get_test_data_path(file_name: &str) -> PathBuf {
        let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        manifest_dir
            .join("tests")
            .join("data")
            .join(file_name)
    }

    #[test]
    fn test_config_singleton_initialize() {
        // Create a new ConfigSingleton instance
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        
        // Get path to test config file
        let path = get_test_data_path("config_manager_test.yaml");
        
        // Test initialization with valid config file
        let result = config_singleton.initialize(&path);
        assert!(result.is_ok(), "Failed to initialize config: {:?}", result.err());
        
        // Test that initializing again is a no-op and returns Ok
        let second_init = config_singleton.initialize(&path);
        assert!(second_init.is_ok(), "Second initialization should succeed");
    }

    #[test]
    fn test_config_singleton_get_instance() {
        // Create a new ConfigSingleton instance
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        
        // Test that get_instance returns error when not initialized
        let result = config_singleton.get_instance();
        assert!(result.is_err(), "Should return error when not initialized");
        assert_eq!(result.err().unwrap(), "Configuration not initialized");
        
        // Initialize the config
        let path = get_test_data_path("config_manager_test.yaml");
        let init_result = config_singleton.initialize(&path);
        assert!(init_result.is_ok(), "Failed to initialize config");
        
        // Test that get_instance returns the correct config after initialization
        let config = config_singleton.get_instance();
        assert!(config.is_ok(), "Should return Ok after initialization");
        
        let config = config.unwrap();
        assert_eq!(config.name, "Test Application");
        assert_eq!(config.version, Some("1.0.0".to_string()));
        assert_eq!(config.environment, "development"); // Default value
        assert_eq!(config.debug_mode, true);
        assert_eq!(config.timeout_seconds, 0); // Default value for u32
        
        // Check database config
        let db = config.database.as_ref().unwrap();
        assert_eq!(db.host, "localhost");
        assert_eq!(db.port, 5432);
        assert_eq!(db.name, "default_db"); // Default value
        assert_eq!(db.max_connections, 100);
        
        // Check features
        assert_eq!(config.features.enable_logging, true);
        assert_eq!(config.features.enable_caching, false); // Default value
        assert_eq!(config.features.enable_metrics, true);
        assert_eq!(config.features.log_level, "info");
    }

    #[test]
    fn test_config_with_minimal_values() {
        // Create a new ConfigSingleton instance
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        
        // Initialize with minimal config
        let path = get_test_data_path("minimal_config.yaml");
        let init_result = config_singleton.initialize(&path);
        assert!(init_result.is_ok(), "Failed to initialize minimal config");
        
        // Test that get_instance returns the correct config with defaults
        let config = config_singleton.get_instance().unwrap();
        
        assert_eq!(config.name, "Minimal App");
        assert_eq!(config.version, None); // Optional field not provided
        assert_eq!(config.environment, "development"); // Default value
        assert_eq!(config.debug_mode, false); // Default value
        assert_eq!(config.timeout_seconds, 0); // Default value
        assert_eq!(config.database, None); // Optional field not provided
        
        // Check features with all defaults
        assert_eq!(config.features.enable_logging, false); // Default value
        assert_eq!(config.features.enable_caching, false); // Default value
        assert_eq!(config.features.enable_metrics, false); // Default value
        assert_eq!(config.features.log_level, ""); // Default value
    }

    #[test]
    fn test_config_singleton_error_handling() {
        // Create a new ConfigSingleton instance
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        
        // Test initialization with non-existent file
        let result = config_singleton.initialize("non_existent_file.yaml");
        assert!(result.is_err(), "Should return error for non-existent file");
        assert!(result.err().unwrap().contains("Failed to open config file"));
        
        // Create an invalid YAML file for testing
        let invalid_yaml_path = std::env::temp_dir().join("invalid_config.yaml");
        std::fs::write(&invalid_yaml_path, "invalid: yaml: content: - [").unwrap();
        
        // Test initialization with invalid YAML
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        let result = config_singleton.initialize(&invalid_yaml_path);
        assert!(result.is_err(), "Should return error for invalid YAML");
        assert!(result.err().unwrap().contains("Failed to parse YAML"));
        
        // Clean up the temporary file
        let _ = std::fs::remove_file(invalid_yaml_path);
    }

    #[test]
    fn test_config_with_missing_required_field() {
        // Create a YAML file missing a required field
        let invalid_yaml_path = std::env::temp_dir().join("missing_required.yaml");
        std::fs::write(&invalid_yaml_path, "# Missing required 'name' field\nfeatures: {}").unwrap();
        
        // Create a new ConfigSingleton instance
        let config_singleton = ConfigSingleton::<TestConfig>::new();
        
        // Test initialization with missing required field
        let result = config_singleton.initialize(&invalid_yaml_path);
        assert!(result.is_err(), "Should return error for missing required field");
        assert!(result.err().unwrap().contains("Failed to parse YAML"));
        
        // Clean up
        let _ = std::fs::remove_file(invalid_yaml_path);
    }
}
